package cn.nn200433.mqtt.config;

import cn.nn200433.mqtt.core.MyMqttClient;
import cn.nn200433.mqtt.service.IMqttConnectService;
import cn.nn200433.mqtt.service.IMqttHandlerService;
import cn.nn200433.mqtt.service.impl.DefaultMqttConnectServiceImpl;
import cn.nn200433.mqtt.service.impl.DefaultMqttHandlerServiceImpl;
import cn.nn200433.mqtt.service.impl.MqttHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.util.List;

/**
 * mqtt客户机配置
 *
 * @author nn200433
 * @date 2021-07-23 03:39:49
 */
@Slf4j
@EnableConfigurationProperties(value = MqttClientProperties.class)
public class MqttClientAutoConfig {

    /**
     * 注入mqtt连接额外处理服务
     *
     * @return {@link IMqttConnectService }
     * @author nn200433
     */
    @Bean
    @ConditionalOnMissingBean(value = IMqttConnectService.class)
    public IMqttConnectService defaultMqttConnectServiceImpl() {
        return new DefaultMqttConnectServiceImpl();
    }

    /**
     * 注入mqtt默认处理服务
     *
     * @return {@link IMqttHandlerService }
     * @author nn200433
     */
    @Bean
    public IMqttHandlerService defaultMqttHandlerServiceImpl() {
        return new DefaultMqttHandlerServiceImpl();
    }

    /**
     * 注入mqtt默认处理服务
     *
     * @return {@link IMqttHandlerService }
     * @author nn200433
     */
    @Bean
    @ConditionalOnBean(value = {IMqttConnectService.class, IMqttHandlerService.class})
    public MqttHandlerContext mqttHandlerContext(IMqttConnectService connectService,
                                                 List<IMqttHandlerService> handlerServiceList) {
        return new MqttHandlerContext(connectService, handlerServiceList);
    }

    /**
     * 注入 mqtt 客户端
     *
     * @param mqttConfig         mqtt配置
     * @param mqttHandlerContext mqtt处理程序上下文
     * @return {@link MyMqttClient }
     * @author nn200433
     */
    @Bean
    @ConditionalOnBean(value = MqttHandlerContext.class)
    @ConditionalOnProperty(prefix = MqttClientProperties.MQTT_CONFIG_PREFIX, name = "enabled", havingValue = "true")
    public MyMqttClient mqttClient(MqttClientProperties mqttConfig, MqttHandlerContext mqttHandlerContext) {
        return new MyMqttClient(mqttConfig, getMqttConnectOptions(mqttConfig), mqttHandlerContext);
    }

    /**
     * 获取mqtt连接选项
     *
     * @param mqttConfig mqtt配置
     * @return {@link MqttConnectOptions }
     * @author nn200433
     */
    private MqttConnectOptions getMqttConnectOptions(MqttClientProperties mqttConfig) {
        MqttConnectOptions options = new MqttConnectOptions();
        // 设置登录的用户名、密码
        options.setUserName(mqttConfig.getUserName());
        options.setPassword(mqttConfig.getPassword().toCharArray());
        // 设置是否清空session，这里如果设置为false表示服务器会保留客户端的连接记录
        options.setCleanSession(mqttConfig.getCleanSession());
        // 设置超时时间 单位为秒
        options.setConnectionTimeout(mqttConfig.getConnectionTimeout());
        // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线，但这个方法并没有重连的机制
        options.setKeepAliveInterval(mqttConfig.getKeepAliveInterval());
        // 设置断开后重新连接
        options.setAutomaticReconnect(mqttConfig.getAutomaticReconnect());
        return options;
    }

}